<?php
/**
 * @author
 * @version 
 */
require_once 'Zend/Controller/Action.php';
class Admin_CategoryController extends Thaind_Controller_Action
{
	var $_arrParam;
	public function init()
    {
//    	Truyền các tham số nhận được ra view
    	$this->_arrParam = $this->_request->getParams();
    	if (!isset($this->_arrParam['page']) || !$this->_arrParam['page']) {
    		$this->_arrParam['page'] = 1;
    	}
    	if (!isset($this->_arrParam['line_per_page']) || !$this->_arrParam['line_per_page']) {
    		$this->_arrParam['line_per_page'] = 10;
    	}
    	if (!isset($this->_arrParam['os']) || !$this->_arrParam['os']) {
    		$this->_arrParam['os'] = 1;
    	}
    	$this->view->arrParam = $this->_arrParam;
    }

    public function preDispatch(){
		$template_path = TEMPLATE_PATH . "/admin/default/";
		$this->loadTemplate($template_path,'template.ini','template');
    	$auth = Zend_Auth::getInstance();
    	$identity = $auth->getIdentity();
    	if (!$auth->hasIdentity()) {
    		$this->_forward('login','index','admin');//,$this->_arrParam);
    	}
    	$identity = $auth->getIdentity();
    	
    	if (!$identity->admin && !in_array(5,explode("|",$identity->permission))){
    		$this->_redirect("/admin");
    	}
    }
    /**
     * The default action - show product list page
     */
    public function indexAction ()
    {
    	Zend_Loader::loadClass("Admin_Models_CategoryModel");
    	$category_model = new Admin_Models_CategoryModel();
//    	Khai báo biến

    	$db = Zend_Db_Table::getDefaultAdapter();
    	if ($this->_request->isPost()){
    		for ($i = 0; $i < $this->_arrParam['number_app'];$i++){
    			if (isset($this->_arrParam['add_app_' . $i]) && $this->_arrParam['add_app_' . $i] !== ""){
	    			$category_model->insert(array("name"	=> $this->_arrParam['add_app_' . $i],"type"	=> 2));;
    			}
    		}
    		for ($i = 0; $i < $this->_arrParam['number_game'];$i++){
    			if (isset($this->_arrParam['add_game_' . $i]) && $this->_arrParam['add_game_' . $i] !== ""){
	    			$category_model->insert(array("name"	=> $this->_arrParam['add_game_' . $i],"type"	=> 1));;
    			}
    		}
    		for ($i = 1; $i <= $this->_arrParam['max_category'];$i++){
    			if (isset($this->_arrParam['category_' . $i]) && $this->_arrParam['category_' . $i] !== ""){
    				$where = $category_model->getAdapter()->quoteInto("id = ?",$i);
	    			$category_model->update(array('name'	=> $this->_arrParam['category_' . $i]), $where);
    			}
    		}
    	}
    	$mysql = $db->select()
    				->from(array("c"	=> "category"),array('id','name','type'));
    	$this->setPagination($mysql, $this->_arrParam);
    }

    public function deleteAction(){
//    	Load model sử dụng trong action

    	Zend_Loader::loadClass('Admin_Models_CategoryModel');
    	
//    	Khai báo biến

    	$model = new Admin_Models_CategoryModel();
		$where = $model->getAdapter()->quoteInto('id = ?', $this->_arrParam['id']);
    	
//		Tắt view

		$this->_helper->viewRenderer->setNoRender();
//    	Xóa record

		try {
    		$model->delete($where);
    		$this->_redirect('/admin/category');
    	} catch (Exception $e) {
    		echo $e;
    	}
    }
    
    
}